﻿using System;
using System.Collections.Generic;
using System.Text;
using EasyStudy.UI;
using EasyStudy.Properties;
using EasyStudy.Logic.Email;
using EasyStudy.tools;

namespace EasyStudy.Logic.Remind
{
    public class RemindCheck
    {
        private static List<EbbinghausTask> tasksNeedToUpdate;

        public static List<EbbinghausTask> TasksNeedToUpdate
        {
            get { return tasksNeedToUpdate; }
        }

        public RemindCheck()
        {
            settings = new Settings();
        }

        public void CheckNoShowForm()
        {
            List<EbbinghausTask> tasks = EbbTaskDBMgr.GetTodayTasks();
            List<EbbinghausTask> tasksPrev = EbbTaskDBMgr.GetUnfinishedTasksAWeek();
            List<EbbinghausTask> todoTasks = new List<EbbinghausTask>();

            // Get all the today's tasks which need to be updated.
            tasksNeedToUpdate = new List<EbbinghausTask>(tasksPrev);
            tasksNeedToUpdate.AddRange(tasks);

            foreach (EbbinghausTask task in tasks)
            {
                if (DateTime.Now > task.NextTime && task.UpdateTime < task.NextTime)
                {
                    todoTasks.Add(task);
                }
            } 
        }

        public void Check()
        {
            if (!GlobalSettings.AllowPopup) 
            {
                return;
            }

            List<EbbinghausTask> tasks = EbbTaskDBMgr.GetTodayTasks();
            List<EbbinghausTask> tasksPrev = EbbTaskDBMgr.GetUnfinishedTasksAWeek();
            List<EbbinghausTask> todoTasks = new List<EbbinghausTask>();

            // Get all the today's tasks which need to be updated.
            tasksNeedToUpdate = new List<EbbinghausTask>(tasksPrev);
            tasksNeedToUpdate.AddRange(tasks);

            DateTime expectUpdateTime = new DateTime();
            foreach (EbbinghausTask task in tasks)
            {
                // 检查今天是否已经UPDATE
                //if (task.UpdateTime.Year == DateTime.Now.Year &&
                //    task.UpdateTime.Month == DateTime.Now.Month &&
                //    task.UpdateTime.Day == DateTime.Now.Day)
                //{
                //    Console.ForegroundColor = ConsoleColor.Green;
                //    Console.WriteLine("Good! You have updated the task <{0}>.", task.Subject);
                //}
                //else

                expectUpdateTime = task.NextTime.AddSeconds(GlobalSettings.GetDelaySeconds(task.ID));
                if (DateTime.Now > expectUpdateTime && task.UpdateTime < task.NextTime)
                { 
#if DEBUG
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("You have a task need to update.");
                    Console.ForegroundColor = ConsoleColor.Blue;
                    Console.WriteLine(task.ToString());
                    Console.ResetColor();
#endif
                    todoTasks.Add(task);
                }
            }

            remind.ClearControls();

            if (todoTasks.Count > 0)
            {
                // Close the logic for sending email.
                //if (!settings.IsTodayEmailSent)
                //{
                //    SendTasks.SendTasksEmail(todoTasks, "新的任务需要Update!");
                //    settings.IsTodayEmailSent = true;
                //}
                remind.LoadTodayTask(todoTasks);
            }

            if (tasksPrev.Count > 0)
            {
                //if (!settings.IsLastEmailSent)
                //{
                //    SendTasks.SendTasksEmail(tasksPrev, "旧的任务需要Update!");
                //    settings.IsLastEmailSent = true;
                //}
                remind.LoadPreviousTasks(tasksPrev);
                remind.selectLastSelectedTabPage();
                remind.PackSize();
            }

            if (todoTasks.Count + tasksPrev.Count > 0)
            {
                remind.selectLastSelectedTabPage();
                remind.PackSize();

                if(!remind.Visible) 
                {
                    remind.Visible = true;
                }
                Sound.Play();
            }
            else {
                remind.Visible = false;
            }
        }

        private FrmRemind remind = new FrmRemind();
        private Properties.Settings settings; 
    }
}
